package Sdo.util;

import java.io.File;
import org.apache.log4j.Logger;


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import Sdo.pojo.History;

public class addPane {

	//设置图标
			
		ImageView iconimage = new ImageView(new Image("/icon.png", 20, 20, false, false));
	
	// TODO Auto-generated method stub
	
	
	final FileChooser fileChooser = new FileChooser();

	final Logger logger =Logger.getLogger(addPane.class);
	private TextField name;
	private TextField host;
	private TextField port;
	private TextField username;
	private PasswordField password;
	private Button pkey;
	

	private ComboBox<String> proxyType;
	private TextField proxyIp;
	private TextField proxyPort;
	private TextField proxyUser;
	private PasswordField proxyPassword;
	
	public GridPane GetAddPane() {
		
		GridPane grid = new GridPane();
		grid.setAlignment(Pos.CENTER);
		grid.setHgap(5);
		grid.setVgap(10);
		grid.setPadding(new Insets(10, 10, 10, 10));// 设置填充, Insets实例设置矩形区域的四边的一组内偏移量


		name = new TextField();
		name.setPromptText("名称");
		
		
		host = new TextField();
		host.setPromptText("IP");
		
		
		port = new TextField("22");
		port.setPromptText("端口");
		
		username = new TextField();
		username.setPromptText("用户名");
		
		
		
		password = new PasswordField();
		password.setPromptText("密码");

		
		pkey = new Button("选择密钥");
		

    	
    	proxyType = new ComboBox<>();
    	
    	proxyType.getItems().add(0, "代理类型");
    	proxyType.getItems().add(1, "HTTP");
    	proxyType.getItems().add(2, "SOCKS5");
    	proxyType.getItems().add(3, "SOCKS4");
    	proxyType.getSelectionModel().select("代理类型");
    	
    	
    	VBox vbox = new VBox(8); 
    	proxyIp = new TextField();
    	proxyIp.setPromptText("代理IP");
    	

    	proxyPort = new TextField();
    	proxyPort.setPromptText("代理端口");
    	
    	proxyUser = new TextField();
    	proxyUser.setPromptText("代理用户");
    	
    	
    	proxyPassword = new PasswordField();
    	proxyPassword.setPromptText("代理密码");
    	
    	vbox.getChildren().addAll(proxyIp,proxyPort,proxyUser,proxyPassword);
    	
		Button confirm = new Button("确定");
		Button cancle = new Button("取消");
		


	
		grid.add(name, 1, 1);
		grid.add(host, 1, 2);
		grid.add(port, 1, 3);
		grid.add(username, 1, 4);
		grid.add(password, 1, 5);
		grid.add(pkey, 1, 6);
    	grid.add(proxyType, 1, 7);
    	
    	
    	
    	grid.add(vbox, 1, 8);
    	vbox.setDisable(true);
    	
    	proxyType.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				if(proxyType.getSelectionModel().getSelectedIndex()==0) {
					
					vbox.setDisable(true);
				}else {
					vbox.setDisable(false);
				}
				
			}
		});

    	
		HBox panel = new HBox(40);
		panel.setAlignment(Pos.BOTTOM_RIGHT);
		panel.getChildren().addAll(confirm,cancle);
		grid.add(panel, 1, 12);
		
		
		pkey.setOnAction(e->{
			
			File file = fileChooser.showOpenDialog(null);
			 	if (file != null) {
			 		pkey.setText(file.getPath());
			 	}else {
	       	 pkey.setText("选择密钥");
	        }
		
			

		});
		 
		cancle.setOnAction(e->{
			Stage stage = (Stage)confirm.getScene().getWindow();
			stage.close();
    	});
		confirm.setOnAction(e->{
			Stage stage = (Stage)confirm.getScene().getWindow();
			String pk="";
			if(pkey.getText()!="选择文件") {
				pk=pkey.getText();
			}
			History history=new History(0, name.getText(), host.getText(), Integer.parseInt(port.getText()),
					username.getText(), password.getText(), pk,proxyType.getValue(),proxyIp.getText(),proxyPort.getText(),
    				proxyUser.getText(),proxyPassword.getText(),iconimage);
//			

			SqliteUtil sq=new SqliteUtil();
		 
				sq.save("history",history);
				stage.close();
				
				
		 
			
			
		});
		
		return grid;
		
	}

}
